package com.ibeeking.found.msg.rest.config.bean;

import com.ibeeking.found.common.utils.UserUtils;
import com.ibeeking.found.msg.rest.service.IOpenService;
import com.ibeeking.found.msg.rest.service.mpevent.*;
import me.chanjar.weixin.common.api.WxConsts;
import me.chanjar.weixin.mp.api.WxMpMessageRouter;
import me.chanjar.weixin.open.api.WxOpenMpService;
import org.springframework.boot.context.properties.ConfigurationProperties;

import javax.annotation.Resource;

import static com.ibeeking.found.common.constants.WxMpConstant.EVENT;
import static me.chanjar.weixin.common.api.WxConsts.EventType.SUBSCRIBE;
import static me.chanjar.weixin.common.api.WxConsts.EventType.UNSUBSCRIBE;
import static me.chanjar.weixin.mp.constant.WxMpEventConstants.CustomerService.*;
import static me.chanjar.weixin.mp.constant.WxMpEventConstants.POI_CHECK_NOTIFY;

/**
 * 微信公众号相关服务自动注册.
 *
 * @author someone
 */
@ConfigurationProperties
public class MpEventConf {

    @Resource
    private IOpenService wxOpenServiceImpl;
    @Resource
    private CsSessionHandler csSessionHandler;
    @Resource
    private LocationHandler locationHandler;
    @Resource
    private LogHandler logHandler;
    @Resource
    private MenuHandler menuHandler;
    @Resource
    private MsgHandler msgHandler;
    @Resource
    private NullHandler nullHandler;
    @Resource
    private ScanHandler scanHandler;
    @Resource
    private StoreCheckNotifyHandler storeCheckNotifyHandler;
    @Resource
    private SubscribeHandler subscribeHandler;
    @Resource
    private UnsubscribeHandler unsubscribeHandler;

    public WxMpMessageRouter messageRouter() {
        Long tenantId = UserUtils.getTenantID();
        WxOpenMpService wxMpService = wxOpenServiceImpl.getWxOpenMpService(tenantId);

        final WxMpMessageRouter newRouter = new WxMpMessageRouter(wxMpService);

        // 记录所有事件的日志 （异步执行）
        newRouter.rule().handler(logHandler).next();

        // 接收客服会话管理事件
        newRouter.rule().async(false).msgType(EVENT).event(KF_CREATE_SESSION)
                .handler(csSessionHandler).end();
        newRouter.rule().async(false).msgType(EVENT).event(KF_CLOSE_SESSION)
                .handler(csSessionHandler).end();
        newRouter.rule().async(false).msgType(EVENT).event(KF_SWITCH_SESSION)
                .handler(csSessionHandler).end();

        // 门店审核事件
        newRouter.rule().async(false).msgType(EVENT).event(POI_CHECK_NOTIFY).handler(storeCheckNotifyHandler).end();

        // 自定义菜单事件
        newRouter.rule().async(false).msgType(EVENT).event(WxConsts.EventType.CLICK).handler(menuHandler).end();

        // 点击菜单连接事件
        newRouter.rule().async(false).msgType(EVENT).event(WxConsts.EventType.VIEW).handler(nullHandler).end();

        // 关注事件
        newRouter.rule().async(false).msgType(EVENT).event(SUBSCRIBE).handler(subscribeHandler).end();

        // 取消关注事件
        newRouter.rule().async(false).msgType(EVENT).event(UNSUBSCRIBE).handler(unsubscribeHandler).end();

        // 上报地理位置事件
        newRouter.rule().async(false).msgType(EVENT).event(WxConsts.EventType.LOCATION).handler(locationHandler).end();

        // 接收地理位置消息
        newRouter.rule().async(false).msgType(WxConsts.XmlMsgType.LOCATION).handler(locationHandler).end();

        // 扫码事件
        newRouter.rule().async(false).msgType(EVENT).event(WxConsts.EventType.SCAN).handler(scanHandler).end();

        // 默认
        newRouter.rule().async(false).handler(msgHandler).end();

        return newRouter;
    }


}
